package seTOee.reflact.instence;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class SimpleFrame {
    public static void add(Object obj) throws Exception {
        FileOutputStream fos = new FileOutputStream("Day010/src/seTOee/reflact/instence/fields.txt",true);
        PrintStream ps = new PrintStream(fos);
        //1、获取obj对象对应的Class对象
        Class cls = obj.getClass();
        ps.println("========"+cls.getSimpleName()+"========");
        //2、获取obj对象所有的属性
        Field[] fields = cls.getDeclaredFields();
        //3、遍历属性,添加操作
        for (Field field : fields) {
            String fieldName = field.getName();
            field.setAccessible(true);
            Object fieldValue = field.get(obj) + "";
            ps.println(fieldName+"=" + fieldValue);
        }
        ps.close();
    }
}
